//
//  InstitutionalAppointmentVM.swift
//  Venus
//
//  Created by point on 2018/10/9.
//  Copyright © 2018年 zhaowang. All rights reserved.
//

import UIKit
import MJRefresh
import SwiftyJSON

class InstitutionalAppointmentVM: NSObject {
    
    weak var vc:InstitutionalAppointmentVC!
    var currentPage:Int = 1
    var newInstitutionAppointModel:[InstitutionAppointModel] =  [InstitutionAppointModel]()
    var historyInstitutionAppointModel:[InstitutionAppointModel] =  [InstitutionAppointModel]()
    
    func loadData() {
        currentPage = 1
        
        let userModel = UserInfo.shareInstance.userModel()
        
        let dict:String = [
            "id":String(userModel.institutionId),
            "isSee":"false" ,//true查询所有已确认过的预约单
            "pageNo":String(currentPage),
            "pageSize":"10",
            "sign":"1",
            "timestamp":"1",
            "operatorId":String(userModel.id)
            ].formatApi()
        let url = kHost + kqueryAppointmentList + dict
        
        TempNetTool.post(url , parameters: nil, progress: nil, success: { [weak self] (result) in
            if let weakSelf = self {
                let josn = JSON(result as Any)
                let tempArr = InstitutionAppointModel.formatModel(json: josn)
                weakSelf.vc.tableView.mj_header.endRefreshing()
                
                if tempArr.count == 0 { //空页面
                    weakSelf.vc.tableView.mj_footer.endRefreshingWithNoMoreData()
                }else {
                    weakSelf.currentPage = 2 //有数据则到了第2页
                }
                
                if tempArr.count < 10  { //没有更多
                    weakSelf.vc.tableView.mj_footer.endRefreshingWithNoMoreData()
                }
                
                if tempArr.count == 10  { //结束刷新
                    weakSelf.vc.tableView.mj_footer.endRefreshing()
                }
                weakSelf.newInstitutionAppointModel = tempArr
                weakSelf.vc.tableView.reloadData()
            }
        }) {[weak self] (error) in
            self?.vc.hudError()
        }
    }
    
    func loadDataMore() {
        let userModel = UserInfo.shareInstance.userModel()
        
        let dict:String = [
            "id":String(userModel.institutionId),
            "isSee":"false" ,//true查询所有已确认过的预约单
            "pageNo":String(currentPage),
            "pageSize":"10",
            "sign":"1",
            "timestamp":"1",
            "operatorId":String(userModel.id)
            ].formatApi()
        let url = kHost + kqueryAppointmentList + dict
        
        TempNetTool.post(url , parameters: nil, progress: nil, success: { [weak self] (result) in
            if let weakSelf = self {
                let josn = JSON(result as Any)
                let tempArr:[InstitutionAppointModel] = InstitutionAppointModel.formatModel(json: josn)
                
                if tempArr.count == 10 { //满页的时候加1
                    weakSelf.currentPage = weakSelf.currentPage + 1
                }
                
                if tempArr.count < 10  { //没有更多
                    weakSelf.vc.tableView.mj_footer.endRefreshingWithNoMoreData()
                }
                
                if tempArr.count == 10  { //结束刷新
                    weakSelf.vc.tableView.mj_footer.endRefreshing()
                }
                weakSelf.newInstitutionAppointModel =   weakSelf.newInstitutionAppointModel  + tempArr
                weakSelf.vc.tableView.reloadData()
            }
        }) {[weak self] (error) in
            self?.vc.hudError()
        }
    }
    
}

extension InstitutionalAppointmentVM {
    func loadDataHistory() {
        currentPage = 1
        
        let userModel = UserInfo.shareInstance.userModel()
        
        let dict:String = [
            "id":String(userModel.institutionId),
            "isSee":"true" ,//true查询所有已确认过的预约单
            "pageNo":String(currentPage),
            "pageSize":"10",
            "sign":"1",
            "timestamp":"1",
            "operatorId":String(userModel.id)
            ].formatApi()
        let url = kHost + kqueryAppointmentList + dict
        
        TempNetTool.post(url , parameters: nil, progress: nil, success: { [weak self] (result) in
            if let weakSelf = self {
                let josn = JSON(result as Any)
                let tempArr = InstitutionAppointModel.formatModel(json: josn)
                weakSelf.vc.tableView.mj_header.endRefreshing()
                
                if tempArr.count == 0 { //空页面
                    weakSelf.vc.tableView.mj_footer.endRefreshingWithNoMoreData()
                }else {
                    weakSelf.currentPage = 2 //有数据则到了第2页
                }
                
                if tempArr.count < 10  { //没有更多
                    weakSelf.vc.tableView.mj_footer.endRefreshingWithNoMoreData()
                }
                
                if tempArr.count == 10  { //结束刷新
                    weakSelf.vc.tableView.mj_footer.endRefreshing()
                }
                weakSelf.historyInstitutionAppointModel = tempArr
                weakSelf.vc.tableView.reloadData()
            }
        }) {[weak self] (error) in
            self?.vc.hudError()
        }
    }
    
    func loadDataMoreHistory() {
        let userModel = UserInfo.shareInstance.userModel()
        
        let dict:String = [
            "id":String(userModel.institutionId),
            "isSee":"true" ,//true查询所有已确认过的预约单
            "pageNo":String(currentPage),
            "pageSize":"10",
            "sign":"1",
            "timestamp":"1",
            "operatorId":String(userModel.id)
            ].formatApi()
        let url = kHost + kqueryAppointmentList + dict
        
        TempNetTool.post(url , parameters: nil, progress: nil, success: { [weak self] (result) in
            if let weakSelf = self {
                let josn = JSON(result as Any)
                let tempArr:[InstitutionAppointModel] = InstitutionAppointModel.formatModel(json: josn)
                
                if tempArr.count == 10 { //满页的时候加1
                    weakSelf.currentPage = weakSelf.currentPage + 1
                }
                
                if tempArr.count < 10  { //没有更多
                    weakSelf.vc.tableView.mj_footer.endRefreshingWithNoMoreData()
                }
                
                if tempArr.count == 10  { //结束刷新
                    weakSelf.vc.tableView.mj_footer.endRefreshing()
                }
                weakSelf.historyInstitutionAppointModel =   weakSelf.historyInstitutionAppointModel  + tempArr
                weakSelf.vc.tableView.reloadData()
            }
        }) {[weak self] (error) in
            self?.vc.hudError()
        }
    }
}

